package com.zzh.partnersys.controller;

import com.zzh.partnersys.common.exception.ResultUtils;
import com.zzh.partnersys.utils.CodeUtils;
import com.zzh.partnersys.utils.EmailRightUtils;
import com.zzh.partnersys.service.EmailService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author �?�?�?
 * @version 1.0
 */
@RestController
@RequestMapping("/email")
public class EmailController {

    @Resource
    private EmailService emailService;

    //请求邮箱
    @GetMapping("/sendCode")
    public ResultUtils sendEmailCode(@RequestParam("toEmail") String toEmail){
        if (StringUtils.isBlank(toEmail)){
            return ResultUtils.fail("邮箱不能为空");
        }

        //判断邮箱是否正确
        boolean rightEmail = EmailRightUtils.isRightEmail(toEmail);
        if (!rightEmail){
            ResultUtils.fail("邮箱不正确不合规");
        }

        //生成6位的随机验证码
        String code = CodeUtils.getCodeToSix();
        String subject = "码猴网注册验证码";
        boolean isSuccess = emailService.sendMsg(toEmail, subject, code);
        if (isSuccess){
            return ResultUtils.ok("验证码已发送至邮箱");
        }else {
            return ResultUtils.fail("邮箱不正确不合规");
        }
    }

}
